package com.lvyq.javaprint.service;

import cn.hutool.json.JSONUtil;
import com.lvyq.javaprint.common.GlobalCode;
import com.lvyq.javaprint.exception.GlobalCache;
import com.lvyq.javaprint.model.PrintData;
import com.lvyq.javaprint.util.ImgPrintUtils;
import com.lvyq.javaprint.util.PrintUtils;
import com.lvyq.javaprint.ws.WebSocketServer;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;
import org.springframework.web.multipart.MultipartFile;

import javax.print.*;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import java.awt.print.PrinterJob;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author lvyq
 * @version 1.0
 * Create by 2024-08-06 16:17
 */
public class ImgPrintService implements PrintService {
    private final ExecutorService executorService = Executors.newFixedThreadPool(10); // 创建线程池


    @Override
    public void print(String text, PrintData data) {
        throw  new UnsupportedOperationException("ImgPrintService 打印服务不支持文本打印");
    }

    @Override
    public void print(InputStream inputStream, PrintData data) {

    }

    @Override
    public void print(MultipartFile multipartFile, PrintData data) {
        InputStream fileInputStream = ImgPrintUtils.getFileInputStream(multipartFile,data.getSessionId());
        asyncPrint(fileInputStream,data);
    }

    @Override
    public void print(File file, PrintData data) {
        FileInputStream fileInputStream = ImgPrintUtils.getFileInputStream(file,data.getSessionId());
        asyncPrint(fileInputStream,data);
    }

    @Override
    public void print(URL url, PrintData data) {
        InputStream fileInputStream = ImgPrintUtils.getFileInputStream(url,data.getSessionId());
        asyncPrint(fileInputStream,data);
    }


    /**
     * @description  图片打印
     * @author lvyq
     * @param[1] document
     * @param[2] data
     * @throws
     * @time 2025-06-13 15:54
     */
    private void asyncPrint(InputStream document, PrintData data){
        // 创建打印请求属性集合
        PrintRequestAttributeSet attributes = PrintUtils.handleAttributeWithPrintData(data);
        executorService.submit(() -> {
            try {

                //获取当前选中的打印机
                javax.print.PrintService curPrintService =(javax.print.PrintService) GlobalCache.get("defaultPrintService");

                WebSocketServer.SendMessageById(JSONUtil.toJsonStr(GlobalCode.PRINT_BEFORE),data.getSessionId());
                // 创建打印
                DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;
                Doc doc = new SimpleDoc(document, flavor, null);

                //打印
                DocPrintJob job = curPrintService.createPrintJob();
                job.print(doc,attributes);

                WebSocketServer.SendMessageById(JSONUtil.toJsonStr(GlobalCode.PRINT_START),data.getSessionId());

            } catch ( Exception e) {
                WebSocketServer.SendMessageById(JSONUtil.toJsonStr(GlobalCode.PRINT_EXCEPTION),data.getSessionId());
            } finally {
                closeDocument(document);
            }
        });
    }

    private void closeDocument(InputStream document) {

        try {
            document.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
